package com.mofabot.bot

import com.mofabot.utils.LogUtil.Companion.log
import org.telegram.telegrambots.meta.api.objects.Update
import java.lang.reflect.Method


/**
 * 命令接口，每一个命令都需要实现这个接口
 *
 * @since 2023/09/19.
 * @author ll
 */
abstract class ICommandBase {

    var callBackMethodMap: MutableMap<String, Method> = mutableMapOf()
        get() = field
        set(value) {
            field = value
        }

    /**
     * callbackPath 用于构造inline消息回调的路由path标识和data
     */
    fun getCallbackPath(method: String, data: String): String {
        if (data.isNotEmpty())
            return this.javaClass.simpleName + ":" + method + "->" + data
        return this.javaClass.simpleName + ":" + method
    }

    /**
     * callbackParam 用于获取inline消息回调的路由传递的参数
     */
    fun getCallbackData(callbackData: String): String {
        if (callbackData.isNotEmpty()) {
            val data = callbackData.split("->")
            if (data.size > 1) {
                return data[1]
            }
        }
        return ""
    }

    /**
     * 命令处理入口
     */
    abstract fun handleCommand(update: Update)

    /**
     * 回调处理入口
     */
    fun handleQuery(update: Update) {
        log.info("InlineKeyboardMarkup Query Param: ${update.callbackQuery.data}")
        val method = update.callbackQuery.data.split(":")[1].split("->")[0]
        callBackMethodMap[method]?.invoke(this, update)
    }
}
